package com.kjjt.controller;

import com.kjjt.entity.UserDemo;
import com.kjjt.service.UserService;
import com.kjjt.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Validated
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public Result addUser(@RequestBody @Valid UserDemo user) {
        userService.addUser(user);
        return Result.success();
    }

    @GetMapping("/get")
    public Result getUser(@NotBlank(message = "查询用户时，用户名不能为空！") String username) {
        UserDemo user = userService.getUser(username);
        return Result.success(user);
    }

    @PostMapping("/delete")
    public Result deleteUser(@NotBlank(message = "删除用户时，用户名不能为空！") String username) {
        userService.deleteUser(username);
        return Result.success();
    }

    @PostMapping("/update")
    public Result updateUser(@RequestBody @Valid UserDemo userDemo) {
        userService.updateUser(userDemo);
        return Result.success();
    }

    @GetMapping("/getAll")
    public Result getAll() {
        List<UserDemo> userDemoList = userService.getAll();
        return Result.success(userDemoList);
    }

}
